package com.right.health.app.ui.user;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.common.SendAuthCodeCommon;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.common.ArticleDetailActivity;

/*
 * Created by hopeliao on 2019-09-24
 * 注册
 *
 */
public class RegisterActivity extends BasePresenterActivity implements View.OnClickListener, OnHttpListener {
    private static final int HTTP_CODE_GET_CODE = 1;
    private static final int HTTP_CODE_VERIFY = 2;
    private static final int HTTP_CODE_DEL = 3;

    private CheckBox mCheckBox;

    private SendAuthCodeCommon mSendAuthCodeCommon;

    private TextView mVerificationTv,mAgreementTv;

    private EditText mPhoneEt, mCodeEt;

    private UserApi mUserApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.register);
    }

    @Override
    public void findView() {
        mCodeEt = (EditText) findViewById(R.id.et_code);
        mPhoneEt = (EditText) findViewById(R.id.et_phone);

        findViewById(R.id.btn_verification).setOnClickListener(this);
        findViewById(R.id.btn_next).setOnClickListener(this);
        findViewById(R.id.btn_del).setOnClickListener(this);

        mCheckBox = (CheckBox) findViewById(R.id.checkbox);
        mCheckBox.setOnClickListener(this);

        mVerificationTv = findViewById(R.id.btn_verification);
        mVerificationTv.setOnClickListener(this);

        findViewById(R.id.btn_next).setOnClickListener(this);
        mAgreementTv=findViewById(R.id.tv_agreement);

        SpannableString spannableString = new SpannableString("登录即代表您已阅读并同意《服务协议》和《隐私协议》");
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_09C3BE)), 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(
                new ClickableSpan() {
                    @Override
                    public void onClick(View v) {
                        ArticleDetailActivity.makeActivity(RegisterActivity.this, "服务协议", AppConfig.HTTP_ADDRESS + "/pandWx/agreement/h5_provisions_declare.html");
                    }
                }, 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_09C3BE)), 19, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(
                new ClickableSpan() {
                    @Override
                    public void onClick(View v) {
                        ArticleDetailActivity.makeActivity(RegisterActivity.this, "隐私协议", AppConfig.HTTP_ADDRESS + "/pandWx/agreement/h5_user_privacy.html");
                    }
                }, 19, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mAgreementTv.setText(spannableString);
        mAgreementTv.setMovementMethod(new LinkMovementMethod());
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        mSendAuthCodeCommon = new SendAuthCodeCommon(milliseconds -> {
            mVerificationTv.setText(milliseconds == 0 ? getString(R.string.again_send_code) :
                    String.format(getString(R.string.format_seconds),
                            String.valueOf(milliseconds / 1000)));

            if (milliseconds == 0) {
                mVerificationTv.setClickable(true);
            }
        });
    }

    private boolean verifyEnter() {
        String phone = mPhoneEt.getText().toString().trim();
        if (!VerifyUtil.isMobile(phone)) {
            ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
            return false;
        }
        String code = mCodeEt.getText().toString().trim();
        if(TextUtils.isEmpty(code)) {
            ToastUtil.show(App.getInstance(),R.string.please_enter_code);
            return false;
        }
        if(!mCheckBox.isChecked()) {
            ToastUtil.show(App.getInstance(),"请同意服务协议");
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                if(verifyEnter()) {
                    String phone = mPhoneEt.getText().toString().trim();
                    String code = mCodeEt.getText().toString().trim();
                    mUserApi.addRequestCode(HTTP_CODE_VERIFY);
                    mUserApi.verifyRegisterCode(phone, code);
                }
//                startIntent(RegisterStep2Activity.class);
                break;
            case R.id.btn_verification:
                mVerificationTv.setClickable(false);
                String phone = mPhoneEt.getText().toString().trim();
                if (!VerifyUtil.isMobile(phone)) {
                    mVerificationTv.setClickable(true);
                    ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
                    return;
                }
                mUserApi.addRequestCode(HTTP_CODE_GET_CODE);
                mUserApi.getVerificationCode(phone);
                break;
            case R.id.btn_del:
                String mobile = mPhoneEt.getText().toString().trim();
                if (!VerifyUtil.isMobile(mobile)) {
                    ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
                    return;
                }
                mUserApi.addRequestCode(HTTP_CODE_DEL);
                mUserApi.delAccount(mobile);
                break;
            case R.id.tv_agreement:
                ArticleDetailActivity.makeActivity(RegisterActivity.this,"服务协议", AppConfig.HTTP_ADDRESS+"/pandWx/agreement/h5_provisions_declare.html");
                break;

        }
    }

    @Override
    public void onResponse(Request request, Response response) {

        if (response.isResponseOK()) {
            if(request.requestCode == HTTP_CODE_GET_CODE) {
                ToastUtil.show(App.getInstance(), "验证码发送成功");
                mSendAuthCodeCommon.startCountdown();
            } else if(request.requestCode == HTTP_CODE_VERIFY) {
                if(response.targetData instanceof AppUserResponse) {
                    AppUserResponse info = response.cast(AppUserResponse.class);

                    Bundle bundle = new Bundle();
                    bundle.putParcelable(BroadCastKey.DATA, info.user);
                    startIntent(RegisterStep2Activity.class,bundle);
                    finish();
                }
            } else if(request.requestCode == HTTP_CODE_DEL) {
                ToastUtil.show(App.getInstance(), "删除成功");
            }

        } else {
            if(request.requestCode == HTTP_CODE_GET_CODE) {
                mVerificationTv.setClickable(true);
            }
            ToastUtil.show(App.getInstance(), response.error);
        }
    }
}
